package com.yp.demo.feign;

import com.yp.demo.config.OAuth2FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;

/**
 * Created by CrazyMouse on 2017/7/20.
 */
@FeignClient(name = "microsrvice-shop-resource",
        configuration = {OAuth2FeignConfiguration.class}, fallbackFactory = ShopFeign.ShopFeignFallbackFactory.class)
public interface ShopFeign {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    String getIndex();

    @RequestMapping(value = "/shop_user", method = RequestMethod.GET)
    Object getUser();

    @Component
    class ShopFeignFallbackFactory implements FallbackFactory<ShopFeign> {

        private static final Logger LOGGER = LoggerFactory.getLogger(ShopFeignFallbackFactory.class);


        @Override
        public ShopFeign create(Throwable throwable) {
            return new ShopFeign() {
                @Override
                public String getIndex() {
                    LOGGER.info("fallback reason was: ", throwable);
                    return "资源服务器宕机";
                }

                @Override
                public Object getUser() {
                    LOGGER.info("fallback reason was: ", throwable);
                    HashMap<String, Object> json = new HashMap<>();
                    json.put("code", 0);
                    json.put("message", "获取失败");
                    return json;
                }
            };
        }
    }
}
